package com.example.wangning.mycarofcountry.tool;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * Created by dllo on 16/1/19.
 */
public class NetworkAvailableUtils {
    private Context context;
    private NetworkAvailableUtils(Context context) {
        this.context = context;
    }
    public static boolean isNetworkAvailable(Context context) {
        //获取手机所有链接管理对象(wife  net)
        //   Context context = activity.getApplicationContext();
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager == null) {
            return false;
        } else {
            //获取NetWorkInfo对象
            //  Network[] networkInfos = connectivityManager.getAllNetworks();
            NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
            if (networkInfo != null && networkInfo.length > 0) {
                for (int i = 0; i < networkInfo.length; i++) {
                    System.out.println(i + "===状态===" + networkInfo[i].getState());
                    System.out.println(i + "===类型===" + networkInfo[i].getTypeName());
                    // 判断当前网络状态是否为连接状态
                    if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
}
